<html>
  <head>
    <title>Narcissus Test Suit</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    
    <script type="application/javascript" >
        function AssertError(message) {
            this.name = "AssertError";
            this.message = message || "Assertion failed";
        }
        AssertError.prototype = new Error();
        AssertError.prototype.constructor = AssertError;

        /*
        * assertEq(actual, expected [, message])
        *   Throw if the two arguments are not the same.  The sameness of two values
        *   is determined as follows.  If both values are zero, they are the same iff
        *   their signs are the same.  Otherwise, if both values are NaN, they are the
        *   same.  Otherwise, they are the same if they compare equal using ===.
        * see https://bugzilla.mozilla.org/show_bug.cgi?id=480199 and
        *     https://bugzilla.mozilla.org/show_bug.cgi?id=515285
        */
        var assertEquals =
            function (actual, expected, message)
            {
                function SameValue(v1, v2)
                {
                    if (v1 === 0 && v2 === 0)
                        return 1 / v1 === 1 / v2;
                    if (v1 !== v1 && v2 !== v2)
                        return true;
                    return v1 === v2;
                }
                if (!SameValue(actual, expected))
                {
                    throw new AssertError('Assertion failed: got "' + actual + '", expected "' + expected + '"' +
                            (message ? ": " + message : ""));
                }
            };

        var assertNotEquals =
            function (actual, expected, message)
            {
                function SameValue(v1, v2)
                {
                    if (v1 === 0 && v2 === 0)
                        return 1 / v1 === 1 / v2;
                    if (v1 !== v1 && v2 !== v2)
                        return true;
                    return v1 === v2;
                }
                if (SameValue(actual, expected))
                {
                    throw new AssertError('Assertion failed: got "' + actual + '", did not expect "' + expected + '"' +
                            (message ? ": " + message : ""));
                }
            };

        function assertTrue(actual, message) {
            assertEquals(actual, true, message);
        }

        function assertFalse(actual, message) {
            assertEquals(actual, false, message);
        }

        function reportSucces(id) {
            document.getElementById(id).textContent = "Succes!";
            document.getElementById(id).style.backgroundColor = "green";
        }

        function reportFailure(id, e) {
            document.getElementById(id).textContent = "Failed: " + e.name + ": " + e.message;
            document.getElementById(id).style.backgroundColor = "red";
        }
    </script>
    
  </head>
  <body>
      <div id="content">
          <table>
              <tbody>
                  <tr>
                      <td colspan="2" >document tests</td>
                  </tr>
                  <tr>
                      <td>document.getElementById()</td><td id="getElementByIdResult" style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>document.getElementsByTagName()</td><td id="getElementsByTagNameResult" style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>document.cookie</td><td id="cookieResult"style="background-color: yellow">Pending</td>
                  </tr>   
                  <tr>
                      <td>Node.appendChild()</td><td id="appendChildResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.removeChild()</td><td id="removeChildResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.hasChildNodes()</td><td id="hasChildNodesResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.replaceChild()</td><td id="replaceChildResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.hasAttributes()</td><td id="hasAttributesResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.isSameNode()</td><td id="isSameNodeResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td>Node.isEqualNode()</td><td id="isEqualNodeResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td colspan="2" >XMLHttpRequest tests</td>
                  </tr>
                  <tr>
                      <td>XMLHttpRequest.open()</td><td id="openResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td colspan="2" >WebSocket tests</td>
                  </tr>
                  <tr>
                      <td>WebSocket.open()</td><td id="wopenResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td colspan="2" >Geolocation tests</td>
                  </tr>
                  <tr>
                      <td>geolocation</td><td id="geolocationResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td colspan="2" >localStorage tests</td>
                  </tr>
                  <tr>
                      <td>setItem</td><td id="setItemResult"style="background-color: yellow">Pending</td>
                  </tr>
                  <tr>
                      <td colspan="2" >sessionStorage tests</td>
                  </tr>
                  <tr>
                      <td>setItem</td><td id="ssetItemResult"style="background-color: yellow">Pending</td>
                  </tr>



              </tbody>
          </table>

          <!-- Tags used for testing purposes -->
          <div id="childLess" />

          <div id="parent">
              <div id="child" />
          </div>

          <div id="hasAttribute">
            <div />
          </div>

         <div id="same" />

         <div id="equal" />

      </div>

      <script type="application/javascript" >
          var el;

          /**
           * getElementById() test
           */
          try {
              el = document.getElementById("getElementByIdResult");
              assertEquals(el.textContent, "Pending");
              el = document.getElementById("InexistandElement")
              assertEquals(el, null);
              reportSucces("getElementByIdResult");
          } catch(e) {
              reportFailure("GetElemntByIdResult", e);
          }

          /**
           * getElementsByTagName() test
           */
          try {
              el = document.getElementsByTagName("table");
              assertEquals(el.length, 1);
              el = document.getElementsByTagName("script");
              assertEquals(el.length, 2);
              el = document.getElementsByTagName("form");
              assertEquals(el.length, 0);
              reportSucces("getElementsByTagNameResult");
          } catch(e) {
              reportFailure("GetElemntsByTagNameResult", e);
          }

          /**
           * cookie test
           */
          try {
              document.cookie = "key1=val1";
              document.cookie = "key2=val2";
              assertEquals(document.cookie, "key1=val1; key2=val2");
              reportSucces("cookieResult");
          } catch(e) {
              reportFailure("cookieResult", e);
          }

          /**
           * appendChild() test
           */
          try {
              el = document.getElementById("content");
              var node = document.createElement("div");
              node.setAttribute("id", "addedNode");
              el.appendChild(node);
              assertNotEquals(document.getElementById("addedNode"), null);
              reportSucces("appendChildResult");
          } catch(e) {
              reportFailure("appendChildResult", e);
          }

          /**
           * removeChild() test
           */
          try {
              el = document.getElementById("content");
              var node = document.getElementById("addedNode");
              el.removeChild(node);
              assertEquals(document.getElementById("addedNode"), null);
              reportSucces("removeChildResult");
          } catch(e) {
              reportFailure("removeChildResult", e);
          }

          /**
           * hasChildNodes() test
           */
          try {
              el = document.getElementById("content");
              assertTrue(el.hasChildNodes());
              el = document.getElementById("childLess");
              assertFalse(el.firstChild.hasChildNodes());
              reportSucces("hasChildNodesResult");
          } catch(e) {
              reportFailure("hasChildNodesResult", e);
          }

          /**
           * replaceChild() test
           */
          try {
              el = document.getElementById("parent");
              var child = el.firstChild;
              var newChild = document.createElement("div");
              newChild.setAttribute("id", "newChild");
              el.replaceChild(newChild, child);
              assertEquals(el.firstChild.getAttribute("id"), "newChild");
              reportSucces("replaceChildResult");
          } catch(e) {
              reportFailure("replaceChildResult", e);
          }

          /**
           * hasAttributes() test
           */
          try {
              el = document.getElementById("hasAttribute");
              assertTrue(el.hasAttributes());
              assertFalse(el.firstChild.hasAttributes());
              reportSucces("hasAttributesResult");
          } catch(e) {
              reportFailure("hasAttributesResult", e);
          }

          /**
           * isSameNode() test
           */
          try {
              el = document.getElementById("same");
              var same = document.getElementById("same");
              var equal = document.getElementById("equal");
              assertTrue(el.isSameNode(same));
              assertFalse(el.isSameNode(equal));
              reportSucces("isSameNodeResult");
          } catch(e) {
              reportFailure("isSameNodeResult", e);
          }

          /**
           * isEqualNode() test
           */
          try {
              el = document.getElementById("same");
              var same = document.getElementById("same");
              var equal = document.getElementById("equal");
              var diff = document.getElementById("childLess");
              assertTrue(el.isEqualNode(same));
              assertFalse(el.isEqualNode(equal)); //hun id attribute is anders dus is false verwacht
              assertFalse(el.isEqualNode(diff));
              reportSucces("isEqualNodeResult");
          } catch(e) {
              reportFailure("isEqualNodeResult", e);
          }

          /**
           * open() test
           */
          try {
              var xHROTest = null;

              //xHROTest = new XMLHttpRequest(); code breaks on this line
              reportFailure("openResult", new Error("Can't create the XMLHttpRequest"));
              /*
              alert("2");
              xHROTest.open("GET", "./xHROTest.php", true);
              xHROTest.onreadystatechange = getTestData;
              xHROTest.send(null);*/
          } catch(e) {
              reportFailure("openResult", e);
          }

          function getTestData() {
            if(xHROTest.readyState == 4 && xHROGetMessages.status == 200){
                var xml = xHROGetMessages.responseXML;
                assertEquals(responseXML, "<succes>It works</succes>");
                reportSucces("openResult");
            } else {
                reportFailure("openResult", new AssertError("readyStrate=" + xHROGetMessages.readyState + ", status=" + xHROGetMessages.status));
            }
          }

          /**
           * WebSocket.open() test
           */
          try {
              //var socket = new MozWebSocket(encodeURI('ws://game.example.com:12010/updates'));  code breaks on this line
              reportFailure("wopenResult", new Error("Can't create the WebSocket"));
          } catch(e) {
              reportFailure("wopenResult", e);
          }

          /**
           * geolocation test
           */
          try {
           reportFailure("geolocationResult", new Error("Code breaks when referencing to window.navigator.geolocation"));
           /* if (window.navigator.geolocation) {
                window.navigator.geolocation.getCurrentPosition(function(position) { alert(position.coords.latitude + ", " + position.coords.longitude); });
              } else {
                reportFailure("geolocationResult", new Error("I'm sorry, but geolocation services are not supported by your browser."));
              }*/
          } catch(e) {
              reportFailure("geolocationResult", e);
          }

          /**
           * setItem test
           */
          try {
              reportFailure("setItemResult", new Error("Code freezes when referencing to localStorage"));
              /*localStorage.setItem('testKey', 'testValue'); //code breaks on this line, even typeof(localStorage) freezes
              assertEquals(localStorage.getItem('testKey'), 'testValue');
              reportSucces("setItemResult");*/
          } catch(e) {
              reportFailure("setItemResult", e);
          }

          /**
           * setItem test
           */
          try {
              reportFailure("ssetItemResult", new Error("Code freezes when referencing to sessionStorage"));
              /*sessionStorage.setItem('testKey', 'testValue'); code breaks on this line, even typeof(sessionStorage) freezes
              assertEquals(localStorage.getItem('testKey'), 'testValue');
              reportSucces("ssetItemResult");*/
          } catch(e) {
              reportFailure("ssetItemResult", e);
          }

          try {
            alert(document);
            alert(window.document);
            alert(document.wrappedJSObject);
            assertEquals(document, window.document);
            alert("true");
          } catch(e) {
            alert(e.message);
          }

      </script>
  </body>
</html>
